using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using HCManage.Api.Models;
using HCManage.IBll;
using HCManage.Model;
using HCManage.Api.Models.Fileter;

namespace HCManage.Api.Controllers.BaseInfo
{
    [Route("api/[controller]")]
    [ApiController]
    public class InterfaceController : ControllerBase
    {
        #region 模块
        private readonly ITblInterfaceBll interfaceBll;

        private DataResultModel dataResultModel = new DataResultModel();
        #endregion

        #region 构造方法
        public InterfaceController(ITblInterfaceBll interface_bll)
        {
            this.interfaceBll = interface_bll;
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据键值查询单个接口信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [ResultExceptionFilter]
        public JsonResult Get(string id)
        {
            tbl_interface model = interfaceBll.Get(id);
            dataResultModel.success = true;
            dataResultModel.data = model;
            return new JsonResult(dataResultModel);
        }

        /// <summary>
        /// 查询接口信息
        /// </summary>
        /// <param name="interface_name">接口名稱</param>
        /// <param name="page_no">頁碼編號</param>
        /// <param name="page"></param>
        /// <param name="page_size"></param>
        /// <returns></returns>
        [HttpGet]
        [ResultExceptionFilter]
        public JsonResult List(string interface_name, string sys_no, string page_no, int? page = 1, int? pagesize = 30)
        {
            Tuple<List<tbl_interface>, int> result = interfaceBll.List(interface_name, sys_no, page_no, page.Value, pagesize.Value);
            dataResultModel.success = true;
            dataResultModel.data = result.Item1;
            dataResultModel.total = result.Item2;
            return new JsonResult(dataResultModel);
        }
        #endregion

        #region 提交
        /// <summary>
        /// 添加接口信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        public JsonResult Post(tbl_interface model)
        {
            if (model == null)
            {
                throw new Exception("请传入参数！");
            }
            bool result = interfaceBll.Insert(model);
            if (!result)
            {
                throw new Exception("新增失败");
            }
            dataResultModel.success = true;
            return new JsonResult(dataResultModel);
        }

        /// <summary>
        /// 修改接口信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        [ResultExceptionFilter]
        public JsonResult Put(tbl_interface model)
        {
            if (model == null)
            {
                throw new Exception("请传入参数！");
            }
            bool result = interfaceBll.Update(model);
            if (!result)
            {
                throw new Exception("更新失败");
            }
            dataResultModel.success = true;
            return new JsonResult(dataResultModel);
        }

        /// <summary>
        /// 删除接口信息
        /// </summary>
        /// <param name="interface_id"></param>
        /// <returns></returns>
        [HttpDelete]
        [ResultExceptionFilter]
        public JsonResult Delete(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new Exception("请传入参数！");
            }
            bool result = interfaceBll.Delete(id);
            if (!result)
            {
                throw new Exception("删除失败");
            }
            dataResultModel.success = true;
            return new JsonResult(dataResultModel);
        }
        #endregion
    }
}
